package shajie.shajun.mmhf.Control;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.Random;

/**
 * Created by admin on 2017/11/21.
 */

public class NumAnim {

    //分割多少个数字
    private static final int COUNTPERS = 60;


    public static void startAnim(TextView textV, float num) {
        startAnim(textV, num, 500);
    }

    public static void startAnim(TextView textV, float num, long time) {
        if (num == 0) {
            textV.setText(NumberFormat(num,2));
            return;
        }

        Float[] nums = splitnum(num, (int)((time/1000f)*COUNTPERS));

        Counter counter = new Counter(textV, nums, time);

        textV.removeCallbacks(counter);
        textV.post(counter);
    }

    private static Float[] splitnum(float num, int count) {
        Random random = new Random();
        float numtemp = num;
        float sum = 0;
        LinkedList<Float> nums = new LinkedList<Float>();
        nums.add(0f);
        while (true) {
            float nextFloat = NumberFormatFloat(
                    (random.nextFloat()*num*2f)/(float)count,
                    2);
            System.out.println("next:" + nextFloat);
            if (numtemp - nextFloat >= 0) {
                sum =NumberFormatFloat(sum + nextFloat, 2);
                nums.add(sum);
                numtemp -= nextFloat;
            } else {
                nums.add(num);
                return nums.toArray(new Float[0]);
            }
        }
    }

    static class Counter implements Runnable {

        private final TextView view;
        private Float[] nums;
        //每次刷新间隔
        private static int pertime = 20;
        //变慢的次数
        private static final int num = 8;
        private int i = 0,n=0;
        private float pai=3.1415926f;

        Counter(TextView view,Float[] nums,long time) {
            this.view = view;
            this.nums = nums;
            this.pertime = 20;
        }

        @Override
        public void run() {
            if (i>nums.length-1) {
                /**
                 * 放大
                 */
                view.setTextColor(Color.YELLOW);
                view.setPivotY(view.getHeight()/2);
                view.setPivotX(view.getWidth()/2);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "scaleX", 1f, 2.5f,1f);
                ObjectAnimator alpha2 = ObjectAnimator.ofFloat(view, "scaleY", 1f,2.5f,1f);
                AnimatorSet animatorSet = new AnimatorSet();//组合动画
                animatorSet.setDuration(800);
                animatorSet.setInterpolator(new DecelerateInterpolator());
                animatorSet.play(alpha).with(alpha2);			//两个动画同时开始
                animatorSet.start();

                return;
            }
            view.setText(NumberFormat(nums[i++],2));
            view.removeCallbacks(Counter.this);
            /**
             * 放慢滚动速度
             */
            if (i>nums.length-num) {
                n+=nums.length/num;
                pertime = (int) (500 * Math.sin((double) ((7* pai / 18) * n / nums.length)));
                if (i>nums.length-1){
                    pertime=100;
                }
            }
            view.postDelayed(Counter.this, pertime);
        }
    }
    public static String NumberFormat(float f,int m){
        return String.format("%."+m+"f",f);
    }
    public static float NumberFormatFloat(float f,int m){
        String strfloat = NumberFormat(f,m);
        return Float.parseFloat(strfloat);
    }
}
